package com.fourth.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fourth.dao.ArticleDao;
import com.fourth.dao.BottomArticleDao;
import com.ljx.entity.Article;
import com.ljx.entity.BottomArticle;
import com.ljx.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author：lmy
 * @Date：2023/3/2 10:32
 */
@RestController
@RequestMapping("home/api/article")
public class ApiArticleController {

    @Autowired(required = false)
    private ArticleDao articleDao;

    @Autowired(required = false)
    private BottomArticleDao bottomArticleDao;

    @PostMapping("addArticle")
    public Result addArticle(@RequestBody Article article){
        int row = articleDao.insert(article);
        String id = article.getNavId();
        BottomArticle bottomArticle = new BottomArticle();
        bottomArticle.setId(id);
        String parentId = "1";
        bottomArticle.setParentId(parentId);
        bottomArticleDao.updateById(bottomArticle);
        if (row == 1){
            return new Result(2000,"添加成功");
        }
        return  new Result(5000,"添加失败");
    }

    @GetMapping("getArticleByNavId")
    public Result getArticleByNavId(@RequestParam String navId){
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.eq("nav_id",navId);
        Article article = articleDao.selectOne(wrapper);
        return new Result(2000,"查询成功",article);
    }

    @PostMapping("updateArticleById")
    public Result updateArticleById(@RequestBody Article article){
        int row = articleDao.updateById(article);
        if (row == 1){
            return new Result(2000,"修改成功");
        }
        return  new Result(5000,"修改失败");
    }
}
